//
//  DCAuthIdentifyController.m
//  Demo
//
//  Created by zhaolin on 2023/1/9.
//

#import "DCAuthIdentifyController.h"
#import "DCAuthInfoUIModel.h"
#import "DCAuthFooterView.h"
#import "DCAuthDrivingController.h"

@interface DCAuthIdentifyController ()<UITableViewDelegate,UITableViewDataSource,DCAuthInfoCommonCellDelegate,DCAuthFooterViewDelegate>

@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) NSIndexPath *currentIndex;

@end

@implementation DCAuthIdentifyController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitleName:@"Identity documents"];
    // Do any additional setup after loading the view.
}

- (void)configureAlloc {
    [super configureAlloc];
    self.step = @"IDENTITY_INFO";
    self.dataSource = @[].mutableCopy;
    NSArray *titleArr = @[@"Driving license",@"Election card",@"NIN"];
    for (int i = 0; i<titleArr.count; i++) {
        DCAuthInfoUIModel *model = [DCAuthInfoUIModel new];
        model.title = [titleArr objectAtIndex:i];
        [self.dataSource addObject:model];
    }
}

- (void)configureUI {
    [super configureUI];
    [self _setCurrntStep:4];
    [self configureTable];
    [self.tableView registerClass:[DCAuthIdentifyCell class] forCellReuseIdentifier:NSStringFromClass(DCAuthIdentifyCell.class)];
//    [self.tableView registerClass:[DCAuthContactsInfoCell class] forCellReuseIdentifier:NSStringFromClass(DCAuthContactsInfoCell.class)];
    [self.tableView registerClass:[DCAuthIdentifyHeader class] forHeaderFooterViewReuseIdentifier:NSStringFromClass(DCAuthIdentifyHeader.class)];
    [self.tableView registerClass:[DCAuthFooterView class] forHeaderFooterViewReuseIdentifier:NSStringFromClass(DCAuthFooterView.class)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.topRadiusView.mas_bottom).mas_equalTo(-30);
    }];
    [self.view bringSubviewToFront:self.tableView];
}
#pragma mark - tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DCAuthInfoUIModel *model = [self.dataSource objectAtIndex:indexPath.row];
    DCAuthIdentifyCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(DCAuthIdentifyCell.class)];
    [cell configureDataWithModel:model index:indexPath];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    DCAuthInfoUIModel *model = [self.dataSource objectAtIndex:indexPath.row];
    return 88;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    DCAuthFooterView *footer = [tableView dequeueReusableHeaderFooterViewWithIdentifier:NSStringFromClass(DCAuthFooterView.class)];
    BOOL contentNull = [self allInfoContentNull];
    [footer setCommitBtnEnabled:!contentNull];
    footer.delegate = self;
    return footer;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 125;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 80;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    DCAuthIdentifyHeader *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:NSStringFromClass(DCAuthIdentifyHeader.class)];
    return header;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DCAuthIdentifyCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    _currentIndex = indexPath;
    [cell setHighlightedBGColor:_currentIndex.row == indexPath.row];
    [self.tableView reloadData];
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    DCAuthIdentifyCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//    [cell setHighlightedBGColor:NO];
}

#pragma mark --- DCAuthFooterViewDelegate
- (void)authFooterViewNextAction:(id _Nullable )sender {
    BOOL contentNull = [self allInfoContentNull];
    if(contentNull) {
        return;
    }
    DCAuthDrivingController *vc = [DCAuthDrivingController new];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)authFooterViewAgreementAction:(id _Nullable)sender {
    [self pushPrivacyAgreement];
}

- (BOOL)allInfoContentNull {
    BOOL contentNull= NO;
    if(!_currentIndex) {
        contentNull = YES;
    }
    return contentNull;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end


@interface DCAuthIdentifyCell ()

@property (nonatomic, strong) UIView *bgView;

@property (nonatomic, strong) UILabel *numLabel;

@property (nonatomic, strong) UILabel *titleLabel;
@end
@implementation DCAuthIdentifyCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self configureUI];
    }
    return self;
}
- (void)configureDataWithModel:(DCAuthInfoUIModel *)model index:(NSIndexPath *)indexPath {
    _titleLabel.text = model.title;
    _numLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row+1];
}
- (void)setHighlightedBGColor:(BOOL)highlighted {
    self.bgView.backgroundColor = highlighted ? [UIColor colorWithString:@"#E1FE97"] : [UIColor colorWithString:@"#F7F7F7"];
}
- (void)configureUI {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = [UIColor colorWithString:@"#FFFFFF"];
    UIView *bgView = [UIView new];
    _bgView = bgView;
    bgView.layer.cornerRadius = 8;
    bgView.layer.masksToBounds = YES;
    bgView.backgroundColor = [UIColor colorWithString:@"#F7F7F7"];
    [self.contentView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(18);
        make.right.mas_equalTo(-17);
        make.top.mas_equalTo(10);
        make.bottom.mas_equalTo(-10);
    }];
    UILabel *numLabel = [UILabel new];
    _numLabel = numLabel;
    numLabel.text = @"1";
    numLabel.textColor = [UIColor colorWithString:@"#000000"];
    numLabel.font = DCFont_PF_Semibold(24);
    [bgView addSubview:numLabel];
    [numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.centerY.mas_equalTo(bgView);
    }];
    
    UILabel *titleLabel = [UILabel new];
    _titleLabel = titleLabel;
    _titleLabel.text = @"#000000";
    titleLabel.textColor = [UIColor colorWithString:@"#1C221B"];
    titleLabel.font =DCFont_PF_Medium(14);
    [bgView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(numLabel.mas_right).mas_offset(17);
        make.centerY.mas_equalTo(bgView);
    }];
    
    //a-ziyuan627
    UIImageView *rightImage = [[UIImageView alloc] init];
    rightImage.image = [UIImage imageNamed:@"a-ziyuan627"];
    [bgView addSubview:rightImage];
    [rightImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(bgView);
        make.right.mas_equalTo(-40);
    }];
}


@end

@implementation DCAuthIdentifyHeader

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self) {
        [self configureUI];
    }
    return self;
}

- (void)configureUI {
    UILabel *label1 = [UILabel new];
    label1.text = @"This verification step can be skipped";
    label1.font = DCFont_PF_Regular(12);
    label1.textColor = [UIColor colorWithString:@"#CED7CD"];
    [self.contentView addSubview:label1];
    [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.top.mas_equalTo(5);
    }];
    
    UILabel *label2 = [UILabel new];
    label2.text = @"*After certification, the quota will be increased";
    label2.font = DCFont_PF_Regular(12);
    label2.textColor = [UIColor colorWithString:@"#658900"];
    [self.contentView addSubview:label2];
    [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(label1.mas_bottom).mas_offset(5);
        make.left.mas_equalTo(label1);
        make.right.mas_equalTo(-30);
    }];
    
    UILabel *label3 = [UILabel new];
    label3.text = @"+₦500～₦1000";
    label3.font = DCFont_PF_Regular(14);
    label3.textColor = [UIColor colorWithString:@"#1C221B"];
    [self.contentView addSubview:label3];
    [label3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(label2.mas_bottom).mas_offset(2);
        make.left.mas_equalTo(label1);
    }];
    
}

@end
